// vue.config.js
const debug = process.env.NODE_ENV !== 'production'

// 引入html-webpack-include-assets-plugin插件
const HtmlWebpackIncludeAssetsPlugin = require('html-webpack-include-assets-plugin')

// 获取外部扩展的版本号，进行固定
const reliances = require('./package');
const vueVersion = reliances.devDependencies['vue'];
const vueRouterVersion = reliances.devDependencies['vue-router'];
const axiosVersion = reliances.devDependencies['axios'];
const elementUIVersion = reliances.devDependencies['element-ui'];
const echartsVersion = reliances.devDependencies['echarts'];
// const vueEChartsVersion = reliances.devDependencies['vue-echarts'];
const vue2editorVersion = reliances.devDependencies['vue2-editor'];
const qsVersion = reliances.devDependencies['^6.7.0'];

//  线上打包的测试环境URL
process.env.BASE_URL = debug ? '/' : '/js/aiw/'
//  本地打包的测试环境URL
// process.env.BASE_URL = debug ? '/' : ''
console.log("当前地址:", process.env.BASE_URL);

module.exports = {
  publicPath: debug ? '/' : '/js/aiw/',
  configureWebpack: config => {
    // 外部扩展，默认为空
    let externals = {}
    // 外部扩展链接及类型，默认为空
    let externalLinks = []
    if (debug) {
      // 开发环境配置
      // 外部扩展相关配置
      externalLinks = [{
        path: `element-ui@${elementUIVersion}/lib/theme-chalk/index.css`,
        type: 'css'
      }]
      externals = {}
    } else {
      // 生产环境配置

      // 外部扩展相关配置
      externalLinks = [{
        path: `vue@${vueVersion}/dist/vue.min.js`,
        type: 'js'
      },
        {
          path: `vue-router@${vueRouterVersion}/dist/vue-router.min.js`,
          type: 'js'
        },
        {
          path: `axios@${axiosVersion}/dist/axios.min.js`,
          type: 'js'
        },
        {
          path: `element-ui@${elementUIVersion}/lib/index.js`,
          type: 'js'
        },
        {
          path: `element-ui@${elementUIVersion}/lib/theme-chalk/index.css`,
          type: 'css'
        },
        {
          path: `echarts@${echartsVersion}/dist/echarts.min.js`,
          type: 'js'
        },
        // {
        //   path: `vue-echarts@${vueEChartsVersion}/dist/vue-echarts.js`,
        //   type: 'js'
        // },
        {
          path: `vue2-editor@${vue2editorVersion}/dist/vue2-editor.js`,
          type: 'js'
        }, {
          path: `qs@${qsVersion}/lib/index.js`,
          type: 'js'
        },
      ]
      externals['vue'] = 'Vue';
      externals['vue-router'] = 'VueRouter';
      externals['axios'] = 'axios';
      externals['element-ui'] = 'ELEMENT';
      externals['echarts'] = 'echarts';
      // externals['vue-echarts'] = 'VueECharts';
      externals['vue2-editor'] = 'Vue2Editor';
      externals['qs'] = 'qs';
    }
    Object.assign(config, {
      // 开发生产共同配置
      externals
    })
    config.plugins = [
      ...config.plugins,
      // html-webpack-include-assets-plugin插件只能在html-webpack-plugin，否则会报错。
      new HtmlWebpackIncludeAssetsPlugin({
        assets: externalLinks,
        // 是否是在 webpack 注入的 js 文件后新增 js 文件？ true 代表 append, false 代表 prepend。
        // 生产环境中，这些 js 应该先加载。
        append: debug,
        publicPath: 'https://unpkg.com/'
      })
    ]
    // config.resolve.alias = {
    //     ...config.resolve.alias,
    //     'src': resolve('src'),
    //     'views': resolve('src/views'),
    //     'styles': resolve('src/styles'),
    //     'components': resolve('src/components'),
    //     'utils': resolve('src/utils'),
    //     'assets': resolve('src/assets'),
    //     'store': resolve('src/store')
    // }
  },
  // 生产环境是否生成 sourceMap 文件
  productionSourceMap: false
}
